package cn.lopr.im.android.mvp.view.activities;

import android.graphics.drawable.Drawable;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.databinding.ActivityInfoBinding;
import cn.lopr.im.android.mvp.contact.IInfoContact;
import cn.lopr.im.android.mvp.presenter.InfoPresenterImpl;
import cn.lopr.im.android.mvp.view.adapters.SpinnerAdapter;
import cn.lopr.im.android.utils.DateUtil;

public class InfoActivity extends Activity implements IInfoContact.IInfoView {

    ActivityInfoBinding binding;
    private User user;
    private boolean isFriend = false;
    private InfoPresenterImpl infoPresenter;

    private final String[] sex = {"男", "女"};
    private String[] year;
    private String[] month;
    private String[] day;

    @Override
    protected View getContentLayoutId() {
        binding = ActivityInfoBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initWidget() {
        super.initWidget();

        this.binding.ivBreak.setOnClickListener(view -> InfoActivity.this.finish());

        this.binding.btnSave.setOnClickListener(view -> this.infoPresenter.save());
    }

    @Override
    protected void initData() {
        super.initData();
        infoPresenter = new InfoPresenterImpl(this, this);

        this.initDate();

        this.user = (User) getIntent().getExtras().getSerializable("user");
        this.isFriend = getIntent().getBooleanExtra("isFriend", false);

        binding.edNickname.setText(this.user.getNickname());
        binding.edIntroduction.setText(this.user.getIntroduction());
        binding.edPhone.setText(this.user.getPhone());

        Glide.with(getApplicationContext()).load(this.user.getPortrait())
                .placeholder(R.drawable.default_head_portrait)
                .dontAnimate()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        binding.ivPortrait.setImageDrawable(resource);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        binding.ivPortrait.setImageDrawable(errorDrawable);
                    }
                });

        this.binding.spSex.setAdapter(new SpinnerAdapter(sex, getApplicationContext()));
        this.binding.spYear.setAdapter(new SpinnerAdapter(year, getApplicationContext()));
        this.binding.spMonth.setAdapter(new SpinnerAdapter(month, getApplicationContext()));
        this.binding.spDay.setAdapter(new SpinnerAdapter(day, getApplicationContext()));

        if (user.getSex().equals("男")) {
            this.binding.spSex.setSelection(0);
        } else {
            this.binding.spSex.setSelection(1);
        }

        if (user.getBirthday() != null) {
            String[] date = DateUtil.formatYMD(user.getBirthday()).split("/");
            this.binding.spYear.setSelection(Integer.parseInt(date[0].replace("年", "")) - 1900);
            this.binding.spMonth.setSelection(Integer.parseInt(date[1].replace("月", "")) - 1);
            this.binding.spDay.setSelection(Integer.parseInt(date[2].replace("日", "")) - 1);

        } else {
            this.binding.spYear.setSelection(90);
            this.binding.spMonth.setSelection(0);
            this.binding.spDay.setSelection(0);

        }

        isDisplay();
    }

    /**
     * 如果是朋友信息，则设置不可见，不可编辑
     *
     * @author shuangyang7
     * @date 2021/1/8 17:25
     */
    void isDisplay() {

        if (this.isFriend) {// 设置为不可编辑
            binding.tvName.setText(this.user.getNickname());
            binding.edNickname.setFocusableInTouchMode(false);
            binding.edNickname.setEnabled(false);
            binding.edNickname.setFocusable(false);
            binding.edNickname.setKeyListener(null);
            binding.edIntroduction.setFocusableInTouchMode(false);
            binding.edIntroduction.setEnabled(false);
            binding.edIntroduction.setFocusable(false);
            binding.edIntroduction.setKeyListener(null);
            binding.edPhone.setFocusableInTouchMode(false);
            binding.edPhone.setEnabled(false);
            binding.edPhone.setFocusable(false);
            binding.edPhone.setKeyListener(null);

            binding.btnBreak.setVisibility(View.GONE);
            binding.btnSave.setVisibility(View.GONE);

            binding.spYear.setEnabled(false);
            binding.spMonth.setEnabled(false);
            binding.spDay.setEnabled(false);
            binding.spSex.setEnabled(false);

        }
    }

    void initDate() {
        year = new String[200];
        month = new String[12];
        day = new String[31];
        int n = 1900;
        for (int i = 0; i < 200; i++) {
            year[i] = n + "年";
            n++;
        }

        int y = 1;
        for (int i = 0; i < 12; i++) {
            if (i < 9) {
                month[i] = "0" + y + "月";
            } else {
                month[i] = y + "月";
            }

            y++;
        }

        int r = 1;
        for (int i = 0; i < 31; i++) {
            if (i < 9) {
                day[i] = "0" + r + "日";
            } else {
                day[i] = r + "日";
            }

            r++;
        }
    }


    @Override
    public void updateSuccess(String message) {

    }

    @Override
    public void updateFailure(String message) {
        this.showVerifyFailed(message);
    }

    @Override
    public String getPhone() {
        return this.binding.edPhone.getText().toString();
    }

    @Override
    public String getName() {
        return this.binding.tvName.getText().toString();
    }

    @Override
    public String getIntroduction() {
        return this.binding.edIntroduction.getText().toString();
    }

    @Override
    public String getBirth() {
        return this.binding.spYear.getSelectedItem().toString() + "-" + this.binding.spMonth.getSelectedItem().toString() + "-" + this.binding.spDay.getSelectedItem().toString();
    }

    @Override
    public String getSex() {
        return this.binding.spSex.getSelectedItem().toString();
    }

}